package android.slc.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * <pre>
 *     author: SLC
 *     blog  : http://blankj.com
 *     time  : 2019/05/03
 * </pre>
 */
public final class SlcFileUtils {

    private static final String[][] MIME_MAP_TABLE = {
            {".3gp", "video/3gpp"}, {".apk", "application/vnd.android.package-archive"}, {".asf", "video/x-ms-asf"}, {".avi",
            "video/x-msvideo"}, {".bin", "application/octet-stream"}, {".bmp", "image/bmp"}, {".c", "text/plain"}, {".class",
            "application/octet-stream"}, {".conf", "text/plain"}, {".cpp", "text/plain"}, {".doc", "application/msword"}, {
            ".docx", "application/msword"}, {".exe", "application/octet-stream"}, {".gif", "image/gif"}, {".gtar",
            "application/x-gtar"}, {".gz", "application/x-gzip"}, {".h", "text/plain"}, {".htm", "text/html"}, {".html",
            "text/html"}, {".jar", "application/java-archive"}, {".java", "text/plain"}, {".jpeg", "image/jpeg"}, {".JPEG",
            "image/jpeg"}, {".jpg", "image/jpeg"}, {".js", "application/x-javascript"}, {".log", "text/plain"}, {".m3u",
            "audio/x-mpegurl"}, {".m4a", "audio/mp4a-latm"}, {".m4b", "audio/mp4a-latm"}, {".m4p", "audio/mp4a-latm"}, {".m4u"
            , "video/vnd.mpegurl"}, {".m4v", "video/x-m4v"}, {".mov", "video/quicktime"}, {".mp2", "audio/x-mpeg"}, {".mp3",
            "audio/x-mpeg"}, {".mp4", "video/mp4"}, {".mpc", "application/vnd.mpohun.certificate"}, {".mpe", "video/mpeg"}, {
            ".mpeg", "video/mpeg"}, {".mpg", "video/mpeg"}, {".mpg4", "video/mp4"}, {".mpga", "audio/mpeg"}, {".msg",
            "application/vnd.ms-outlook"}, {".ogg", "audio/ogg"}, {".pdf", "application/pdf"}, {".png", "image/png"}, {".pps"
            , "application/vnd.ms-powerpoint"}, {".ppt", "application/vnd.ms-powerpoint"}, {".pptx", "application/vnd" +
            ".ms-powerpoint"}, {".prop", "text/plain"}, {".rar", "application/x-rar-compressed"}, {".rc", "text/plain"}, {
            ".rmvb", "audio/x-pn-realaudio"}, {".rtf", "application/rtf"}, {".sh", "text/plain"}, {".tar", "application/x" +
            "-tar"}, {".tgz", "application/x-compressed"}, {".txt", "text/plain"}, {".wav", "audio/x-wav"}, {".wma", "audio/x" +
            "-ms-wma"}, {".wmv", "audio/x-ms-wmv"}, {".wps", "application/vnd.ms-works"},
            {".xml", "text/plain"}, {".z", "application/x-compress"}, {".zip", "application/zip"}, {"", "*/*"}};


    public static final String TIME_FORMAT = "_yyyyMMdd_HHmmss";

    public static String newFileNameByTime(String timeFormatHeader) {
        final Date date = new Date(System.currentTimeMillis());
        //必须要加上单引号
        final SimpleDateFormat dateFormat = new SimpleDateFormat("'" + timeFormatHeader + "'" + TIME_FORMAT,
                Locale.getDefault());
        return dateFormat.format(date);
    }

    /**
     * @param timeFormatHeader 格式化的头(除去时间部分，不要加_)
     * @param extension        后缀名(不要加点)
     * @return 返回时间格式化后的文件名
     */
    public static String newFileNameByTime(String timeFormatHeader, String extension) {
        return newFileNameByTime(timeFormatHeader,extension,true);
    }

    public static String newFileNameByTime(String timeFormatHeader, String extension, boolean appendPointSymbol) {
        return newFileNameByTime(timeFormatHeader) + (appendPointSymbol ? "." : "") + extension;
    }

    /**
     * 获取日期路径
     *
     * @return
     */
    public static String datePath() {
        return SlcTimeUtils.datePath();
    }

    /**
     * 根据一个未知路径获取文件名
     *
     * @param path
     * @return 如果path为空自动生成一个文件名
     */
    public static String getFileNameByUnknownPath(String path) {
        if (StringUtils.isEmpty(path)) {
            return newFileNameByTime("SLC_");
        }
        int lastSetIndex = path.lastIndexOf(File.separator);
        if (lastSetIndex == -1) {
            return path;
        }
        return path.substring(lastSetIndex + 1);
    }

    public static String byte2FitMemorySize(final long byteNum) {
        if (byteNum < 0) {
            return "shouldn't be less than zero!";
        } else if (byteNum < 1024) {
            return String.format(Locale.getDefault(), "%.3fB", (double) byteNum);
        } else if (byteNum < 1048576) {
            return String.format(Locale.getDefault(), "%.3fKB", (double) byteNum / 1024);
        } else if (byteNum < 1073741824) {
            return String.format(Locale.getDefault(), "%.3fMB", (double) byteNum / 1048576);
        } else {
            return String.format(Locale.getDefault(), "%.3fGB", (double) byteNum / 1073741824);
        }
    }

    public static String getSuffixCarryDot(File file) {
        return StrPool.DOT + FileNameUtil.getSuffix(file);
    }

    public static String getSuffixCarryDot(String fileName) {
        return StrPool.DOT + FileNameUtil.getSuffix(fileName);
    }

    /**
     * 补全结尾文件分隔符
     *
     * @param path
     * @return
     */
    public static String complementSeparator(String path) {
        if (!StrUtil.endWith(path, File.separator)) {
            return path + File.separator;
        }
        return path;
    }

    /**
     * 移除结尾文件分隔符
     *
     * @param path
     * @return
     */
    public static String removeSeparator(String path) {
        String result = StrUtil.removeSuffix(path, File.separator);
        return result;
    }

    /**
     * 补全开头文件分隔符
     *
     * @param path
     * @return
     */
    public static String complementSeparatorWithStart(String path) {
        if (!StrUtil.startWith(path, File.separator)) {
            return File.separator + path;
        }
        return path;
    }

    /**
     * 移除开头文件分隔符
     *
     * @param path
     * @return
     */
    public static String removeSeparatorWithStart(String path) {
        String result = StrUtil.removePrefix(path, File.separator);
        return result;
    }

    /**
     * 获取文件执行路径（绝对路径）
     *
     * @return 文件执行路径
     */
    public static String getUserDirPath() {
        return System.getProperty("user.dir");
    }

    /**
     * 获取文件执行目录
     *
     * @return 文件执行目录
     */
    public static File getUserDir() {
        return FileUtil.file(getUserDirPath());
    }

    /**
     * 根据路径获取名称
     * @param path
     * @return
     */
    public static String getNameByPath(String path) {
        if (StrUtil.startWith(path, "http") || StrUtil.startWith(path, "https")) {
            return CollectionUtil.getLast(StrUtil.splitTrim(path, "/"));
        }
        return FileUtil.getName(path);
    }
}
